#!/bin/bash

function read_choose()
{
	local def_choose choose message

	if [ "$2" = "" ] || [ "$2" = "y" ] || [ "$2" = "Y" ]
	then
		def_choose="Y"
		message="$1 [Y/n]? "
	else
		def_choose="N"
		message="$1 [y/N]? "
	fi

	while :
	do
		echo -n "${message}"
		read choose
		[ "${choose}" ] || choose=${def_choose}
		case ${choose} in
			Y | y)
				return 0 ;;
			n | N)
				return 1 ;;
			*)
				echo "Please input y/Y/n/N/Enter" ;;
		esac
	done
}

if [ "$1" ]
then
KERNEL_PATH=$1
else
KERNEL_PATH=${PWD}
fi

MAKE="make -C ${KERNEL_PATH}"

${MAKE} menuconfig || exit 1
read_choose "Do you want to compile the kernel" "Y" || exit 0
rm defconfig -rfv && ${MAKE} savedefconfig || exit 1
${MAKE} -j${MAKE_JOBS} || exit 1
read_choose "Do you want to install the kernel" "Y" || exit 0
sudo ${MAKE} modules_install || exit 1
sudo ${MAKE} install || exit 1
read_choose "Do you want to restart the computer" "N" || exit 0
sudo reboot
